﻿<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta http-equiv="pragma" content="no-cache">  <!-- HTTP 1.1 -->
    <meta http-equiv="cache-control" content="no-cache,must-revalidate">  <!-- HTTP 1.0 -->
    <meta name="renderer" content="webkit|ie-comp|ie-stand" /> <!--优先级：极速模式，兼容模式，IE模式-->
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" />
    <title>扬州房产测绘业务管理系统</title>
    <link rel="stylesheet" type="text/css" href="../../libs/jeasyui-181/themes/default/easyui.css">
    <link rel="stylesheet" type="text/css" href="../../libs/jeasyui-181/themes/icon.css">
    <link rel="stylesheet" type="text/css" href="../../libs/jeasyui-181/themes/color.css">
    <link rel="stylesheet" type="text/css" href="../../css/common.css">
    <script type="text/javascript" src="../../libs/jeasyui-181/jquery.min.js"></script>
    <script type="text/javascript" src="../../libs/jeasyui-181/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="../../libs/thUtil.js"></script>
    <script type="text/javascript">

        var mainGrid = new UserContol();
        
        $(function () {
            if (UrlParm.hasParm('text')) {
                $('h1').html(UrlParm.parm('text'));
            }

            $('#insert_Link').html(button.insert).linkbutton({ onClick: ajax.insert });
            $('#update_Link').html(button.insert).linkbutton({ onClick: ajax.update });
            $('#delete_Link').html(button.insert).linkbutton({onClick: ajax.delete});
            $('#resort_Link').html(button.resort).linkbutton({ onClick: ajax.resort });

            mygrid.init();
        });

        var ajax = {
            getList: function (param, success) {
                Network.connect(window.api.groupSelectTreeGrid, 'POST', param, function (res) {
                    if (res.code == ResponseCode.OK) {
                        success(res.data);
                    }
                });
            },
            getOne: function (param,success) {
                Network.connect(window.api.groupSelectOne, 'POST', param, function (res) {
                    if (res.code == ResponseCode.OK) {
                        success(res.data);
                    }
                });
            },
            getTopDep: function (methods) {
                Network.connect(window.api.groupGetTopDepCombobox, 'POST', {}, methods);
            },
            insert: function () {
                dlg = window.parent.$.hDialog({
                    href: window.router.groupForm + '?n=' + Math.random(),
                    title: '新增部门',
                    width: 700,
                    height: 360,
                    onLoad: function () {
                        var row = mygrid.selectRow();
                        ajax.getTopDep(function (res) {
                            res.data.unshift({id:'0',text:'顶级'});
                          window.parent.$("#G_PARENTID").combobox({ data: res.data });

                            if (row) {
                                var data = window.parent.$("#G_PARENTID").combobox('getData');
                                $.each(data, function (idx, obj) {
                                    if (obj.id == row.GROUPID) {
                                      window.parent.$("#G_PARENTID").combobox('setValue', row.GROUPID);
                                    }
                                });
                            }
                        });

                        globalAjax.sysAreaItems(function (res) {
                          window.parent.$("#GXZQYTYPE").combobox({
                                data: res.data,
                                onLoadSuccess: function () {
                                    var data = window.parent.$("#GXZQYTYPE").combobox("getData");
                                }
                            });
                        });
                      window.parent.$("tr[name='edit']").hide();
                    },
                    submit: function () {
                        var isValid = window.parent.$('#form').form("validate");
                        if (isValid) {

                            var formData = window.parent.$("#form").serializeJson();
                            formData.GROUPTYPE = formData.GROUPTYPE == '1' ? 1 : 2;

                            Network.connect(window.api.groupInsert, 'POST', formData, function (res) {
                                if (res.code == ResponseCode.OK) {
                                    Tools.show('添加成功');
                                    dlg.dialog('close');
                                    mygrid.reload();
                                }
                            });
                        }
                    }
                }); 
            },
            update: function () {
                var rows = $('#mainGrid').datagrid("getSelections");
                if (rows.length == 0) {
                    Tools.alert('请选择需要修改的数据。'); return;
                }
                dlg = window.parent.$.hDialog({
                    href: window.router.groupForm + '?n=' + Math.random(),
                    title: '修改部门',
                    width: 700,
                    height: 360,
                    onLoad: function () {
                        var param = { GROUPID: rows[0].GROUPID }; 
                        var dep, area;
                        ajax.getTopDep(function (res) {
                            res.data.unshift({ id: '0', text: '顶级' });
                          window.parent.$("#G_PARENTID").combobox({ data: res.data });
                            dep = res;
                        });

                        globalAjax.sysAreaItems(function (res) {
                          window.parent.$("#GXZQYTYPE").combobox({
                                data: res.data,
                                onLoadSuccess: function () {
                                    var data = window.parent.$("#GXZQYTYPE").combobox("getData");
                                }
                            });
                            area = res;
                        });

                        var timer = setInterval(function () {
                            if (dep && area) {
                                clearInterval(timer);
                                ajax.getOne(param, function (data) {
                                  window.parent.$('#form').form('load', data);
                                });
                            }
                        }, 200);

                      window.parent.$("tr[name='edit']").hide();
                    },
                    submit: function () {
                        var isValid = window.parent.$('#form').form("validate");
                        if (isValid) {

                            var formData = window.parent.$("#form").serializeJson();
                            formData.GROUPTYPE = formData.GROUPTYPE == '1' ? 1 : 2;

                            Network.connect(window.api.groupUpdate, 'POST', formData, function (res) {
                                if (res.code == ResponseCode.OK) {
                                    Tools.show('修改成功');
                                    dlg.dialog('close');
                                    mygrid.reload();
                                }
                            });
                        }
                    }
                });
            },
            delete: function () {
                var rows = $('#mainGrid').datagrid("getSelections");
                if (rows.length == 1) {
                    Tools.confirm('是否确定删除该数据？', function (result) {
                        if (result) {
                            var formData = { GROUPID: rows[0].GROUPID};
                            Network.connect(window.api.groupDelete, 'POST', formData, function (res) {
                                if (res.code == ResponseCode.OK) {
                                    Tools.show('删除成功');
                                    mygrid.reload();
                                }
                            });
                        }
                    });
                }
                else {
                    Tools.alert('请选择需要删除的数据。');
                }
            },
            resort: function () {
                Tools.confirm('是否确定重新整理数据？', function (result) {
                    if (result) {
                        Network.connect(window.api.groupReSort, 'POST', {}, function (res) {
                            if (res.code == ResponseCode.OK) {
                                Tools.show('重新整理成功');
                                mygrid.reload();
                            }
                        });
                    }
                });
            }
        }

        var mygrid = {
            init: function () {
                $("#mainGrid").treegrid({
                    fit:true,
                    toolbar:'#toolbar',
                    fitColumns: false,
                    width: $(window).width()-22,
                    height: $(window).height() - 110,
                    rownumbers: true,
                    pagination: false,
                    idField: 'GROUPID',
                    treeField: 'G_CNAME',
                    columns: [[
                        { field: 'G_CNAME', title: '部门名称', halign: 'center', align: 'left', width: 350 },
                        { field: 'GXZQYTYPE_NAME', title: '行政区', halign: 'center', align: 'center', width: 120 },
                        {
                            field: 'GROUPTYPE', title: '作业部门', halign: 'center', align: 'center', width: 100, formatter: function (value, row, index) {
                                var returnValue = '<span style="color:{0}">{1}</span>';
                                if (value == '1') {
                                    return returnValue.format('blue','是');
                                }
                                else if (value == '2') {
                                    return returnValue.format('red', '否');
                                }
                                else {
                                    return value;
                                }
                                return Tools.json.findTextById(i18n.GroupType, value);
                            }
                        },
                        { field: 'SORT', title: '排序编号', halign: 'center', align: 'center', width: 90, sortable: true }
                    ]],
                    loader: function (param, success, error) {
                        $('#mainGrid').treegrid('clearSelections');
                        ajax.getList(param, success);
                    }
                });
                $(window).resize(function () {
                    $('#mainGrid').datagrid('resize', {
                        width: $(window).width() - 22,
                        height: $(window).height() - 110
                    })
                });
            },
            reload: function () {
                $('#mainGrid').treegrid('reload');
            },
            selectRow: function () {
                return $('#mainGrid').treegrid('getSelected');
            }
        };

    </script>

</head>
<body>
<div class="container-wrap">
    <div id="toolbar">
        <div class="container-tit">
            <h1></h1>
        </div>
        <div class="sreach fixed">
            <!--<input type="text" class="easyui-textbox" data-options="label:'上级部门:',labelWidth: 80,width:240,"/>
            <a href="#" class="easyui-linkbutton search">查询</a>-->
            <div class="bar-btn">
                <a id="insert_Link" href="#" class="easyui-linkbutton insert">新增</a>
                <a id="update_Link" href="#" class="easyui-linkbutton update">修改</a>
                <a id="delete_Link" href="#" class="easyui-linkbutton delete">删除</a>
                <a id="resort_Link" href="#" class="easyui-linkbutton update">重新整理</a>
            </div>
        </div>
    </div>
    <div id="mainGrid"></div>
</div>
</body>
</html>